package com.tempjob.app.utils;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class JwtTokenUtil {

    /**
     * 生成tokens
     *
     * @param jwtSecret
     * @param infos
     * @return
     */
    public static String buildToken(String jwtSecret, Object infos, Integer expireTime) {
        Date now = new Date();
        Date expiryDate = new Date(now.getTime() + expireTime * 1000);
        return Jwts.builder()
                .setSubject(JSON.toJSONString(infos))
                .setIssuedAt(new Date())
                .setExpiration(expiryDate)
                .signWith(SignatureAlgorithm.HS512, jwtSecret)
                .compact();
    }


}
